<?php
require_once MODULE_ROOT."/class/oauth2.class.php";
global $_W,$_GPC;
load()->func('tpl');
	$acid=$_W['uniacid'];
	$rid=$_GPC["rid"];
	
	//调试
function is_weixin(){

	if ( strpos($_SERVER['HTTP_USER_AGENT'],

			'MicroMessenger') !== false ) {

			return true;

	}

	return false;

}

if(is_weixin()){
	
}else{
	
	$_W["openid"]='oU4go0msnyP2JizVuKdoNoZ1xLqo';
}
	$openid = $_W["openid"];
	$fromid=$_GPC["fromid"];//分享人openid=$_GPC["fromid"];//分享人openid
	$reply=pdo_fetch("SELECT * FROM " . tablename('dt_question_reply') . " WHERE rid = :rid ", array(':rid' => $rid));
	
	//用户表
	$result=pdo_fetch("select count(*) as num from ".tablename("dt_userinfo")." where rid=:rid and points<>''",array(":rid"=>$rid));
	$num = (int)$result['num'];
	$count=$num+(int)$reply["xncount"];
	
	if(!empty($_COOKIE["fromid"])){
		$fromid=$_COOKIE["fromid"];
		setcookie("fromid");
	}
	//当前用户的最后一条记录
	$dtuser = pdo_fetch("SELECT * FROM ". tablename('dt_userpoints')." WHERE openid=:openid and rid=:rid ORDER BY ID DESC LIMIT 1" , array(":openid"=>$openid,":rid"=>$rid));
	$dtuser_points_counts=pdo_fetchcolumn("SELECT sum(points) FROM ". tablename('dt_userpoints')." WHERE openid=:openid and rid=:rid 
			and status=:status " , array(":openid"=>$openid,":rid"=>$rid,":status"=>1));
	$dtuser_points_counts=$dtuser_points_counts ? $dtuser_points_counts :0;
	//echo $dtuser_points_counts; die();
	//$usercount = pdo_fetch("SELECT * FROM ". tablename("dt_usercount")." WHERE openid=:openid and rid=:rid LIMIT 1" , array(":openid"=>$openid,":rid"=>$rid));
	if($reply["subscribe"]==1){
		$nick=$_W['fans']['nickname'];
		$headimg=$_W['fans']['tag']['avatar'];
	}else{
		//调试
		$userinfo=$this->getUserInfo($rid);
		$obj = json_decode($userinfo);
		$nick=$obj->nickname;
		$headimg=$obj->headimgurl;

		
	}
	
	
	
	//添加用户信息
	$userinfo=pdo_fetch("select * from ".tablename("dt_userinfo")." where openid=:openid and rid=:rid",array(":openid"=>$openid,":rid"=>$rid));
	if(!empty($userinfo)){
	
	}else{
		//添加用户信息  dtcount  可答题次数 shyucount
		$pars = array("rid"=>$rid,"iacid"=>$acid, "uniacid"=>$acid,"openid"=>$openid,"nick"=>$nick,
			"headimg"=>$headimg,"addtime"=>time(),"shyucount"=>$reply["dtcount"]);
		pdo_insert("dt_userinfo", $pars);
	}
	$userinfo=pdo_fetch("select * from ".tablename("dt_userinfo")." where openid=:openid and rid=:rid",array(":openid"=>$openid,":rid"=>$rid));
	//可答题次数
	$shyucount=$userinfo["shyucount"];
	
	$dtuser = pdo_fetchall("SELECT * FROM ". tablename('dt_userpoints')."
			 WHERE openid=:openid and rid=:rid " , array(":openid"=>$openid,":rid"=>$rid));
	
	//echo "select count(*) from ims_dt_userpoints where rid=$rid and openid='$openid' and FROM_UNIXTIME(unix_timestamp(time),'%Y-%m-%d')=FROM_UNIXTIME(unix_timestamp(now()),'%Y-%m-%d')";
	$dtuser_count=pdo_fetchcolumn("select count(*) from ims_dt_userpoints where rid=$rid and openid='$openid' and FROM_UNIXTIME(unix_timestamp(time),'%Y-%m-%d')=FROM_UNIXTIME(unix_timestamp(now()),'%Y-%m-%d')");
	//print_r($dtuser); die();
	$shyucount=$shyucount-$dtuser_count;
	
	
	$sql="SELECT * FROM " . tablename('dt_question_reply') . " WHERE rid = :rid LIMIT 1";
	$quesreply = pdo_fetch($sql, array(':rid' => $rid));
	
	$huodongname=$quesreply["huodongname"];    //芝麻开门
	//$followdesc=$quesreply["followdesc"];    //关注说明
	$huodongdesc=$quesreply["huodongdesc"];    //活动规则
	
	if ($quesreply['start_time'] > time()) {
		//倒计时
		$startsecond=$quesreply["start_time"];
		$now = time();
		$leftsecond=$startsecond-$now;
		include $this->template('nostart');
		exit();
	}
	
	if ($quesreply == false || $quesreply['start_time'] > time() || $quesreply['end_time'] < time() || $quesreply['status']==0) {
		include $this->template('over');
		exit();
	}
	
	//是否已分享
	$addtime=date('Y-m-d',time());
	$usershare=pdo_fetch("SELECT * FROM ". tablename("dt_usershare")." WHERE openid=:openid and rid=:rid and addtime=:addtime
			 ORDER BY ID DESC LIMIT 1" , array(":openid"=>$openid,":rid"=>$rid,":addtime"=>$addtime));
	$usershare =$usershare ? 1 : 0;
	//echo $usershare; die();
	//企业信息
// 	$qyname=$quesreply["qyname"];
// 	$qylogo=$quesreply["qylogo"];
// 	$qylink=$quesreply["qylink"];
// 	$homebg=$quesreply["homebg"];
	
	
	
	//$isshareadd=$reply["isshareadd"];//是否 分享增加一次
	
	//如果超过分享次数上限
// 	$sharelist=pdo_fetchall("select * from ".tablename("dt_usershare")." where fromuserid=:fromid and rid=:rid",array(":fromid"=>$openid,":rid"=>$rid));
// 	$is_max=0;
// 	if($reply["maxcount"]!=0 && !empty($reply["maxcount"]) && $reply["maxcount"]<=count($sharelist)){
// 		$is_max=1;
// 	}
	
	
	/**
	 * 分享
	 */
	
	$sharetitle=$reply["sharetitle"];//分享标题
	$sharecontent=$reply["sharecontent"];//分享内容
	//$shareimg=$_W['attachurl'].$reply["shareimg"];//分享图片
	$shareimg=$userinfo['headimgurl'] ? $userinfo['headimgurl'] : $_W['attachurl'].$reply["shareimg"];;//分享图片
	$isshareadd=$reply["isshareadd"];//是否 分享增加一次
	$sharelink="";
	$huodongname=$reply["huodongname"];
	$color=$reply["color"];
	if($isshareadd==0){
		$sharelink = $_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'index', array ('rid' => $rid));
	}else{
		$sharelink=$_W ['siteroot'] . 'app/' . $this->createMobileUrl ( 'index', array ('rid' => $rid,'fromid'=>$openid));
	}
	$sharelink = str_replace ( './', '', $sharelink );//分享链接
	$url='http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'];
	
	$sharetitle="我在珂蓝国际【正青春—占天下】答题活动中赢得80元现金宝藏！来芝麻开门答题赢现金！";
	
	$sharecontent="我已经领取80元宝藏！轻松回答3道题，立得现金奖！这个活动官方认证100％真实有效，全国8068个领到的人都说好";
	$shareimg=$headimg ? $headimg : $userinfo_2['headimg'];//分享图片
	
	//取排行榜
	//排行榜显示数
	$phbnum=(!empty($reply["phbnum"]) && $reply["phbnum"]!=0)?$reply["phbnum"]:100;
	$phbimg=!empty($reply["phbimg"])?$_W['attachurl'].$reply["phbimg"]:MODULE_URL."resource/images/pic/ranking.png";
	//排行榜
	$userlist=pdo_fetchall("select * from ims_dt_userinfo 
			where rid='$rid' and points>0 
 and FROM_UNIXTIME(pointstime,'%Y-%m-%d')=mustdate  
order by pointstime desc limit 0,20 ");
	
	$usersum=count($userlist);
	//print_r($userlist); die();	
	/**
	 * 检查用户是否关注
	 */
// 	if($quesreply["subscribe"]==1){
// 		$from_user = ! empty ( $_W ['openid'] ) ? $_W ['openid'] : $_W ['fans'] ['from_user'];
// 		$account = WeAccount::create ( $_W ['account'] );
// 		$userinfo = $account->fansQueryInfo ( $from_user );
// 		$followdesc=$quesreply["followdesc"];
// 		$followqrcode=$quesreply["qrcode"];
// 		if (!empty($userinfo) && $userinfo["subscribe"]==1) {
// 			include $this->template('index');
// 		}else{
// 			setcookie("fromid",$fromid, time()+3600*12);
// 			include $this->template('subscribe');
// 		}
// 	}else{
		setcookie("nick",$nick);
		setcookie("head",$headimg);
		include $this->template('index');
	//}
	
	exit ();